package com.lzctzk.serviceaddress.controller;

import com.lzctzk.serviceaddress.dao.HouseMapperEx;
import com.lzctzk.serviceaddress.domain.HouseRepository;
import com.lzctzk.serviceaddress.domain.House;
import com.lzctzk.serviceaddress.enums.ResultEnum;
import com.lzctzk.serviceaddress.exception.MyException;
import com.lzctzk.serviceaddress.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Title:
 * Description:
 * Company:
 *
 * @user GuiYue
 * @time 0:15
 * @project_name service-House
 * @package_name com.lzctzk
 **/

@Api("房屋地址信息")
@RestController
@RequestMapping("/house")
public class HouseController
{
    @Autowired
    private HouseRepository houseRepository;
    @Autowired
    private HouseMapperEx houseMapperEx;

    Logger logger = LoggerFactory.getLogger(this.getClass());


    @ApiOperation("新增房屋地址信息")
    @ApiImplicitParam(name = "house",value = "单个房屋地址信息实体",required = true,dataType = "House")
    @PostMapping("")
    public Result insertAddress(@RequestBody House house) throws Exception
    {
        logger.debug("插入数据", house);
        houseRepository.save(house);
        return Result.ok();
    }

    @ApiOperation("删除房屋地址信息")
    @ApiImplicitParam(name = "id",value = "房屋Id",required = true,dataType = "Integer",paramType = "path")
    @DeleteMapping("/{id}")
    public Result deleteAddressById(@PathVariable("id") Integer id) throws Exception
    {
        houseRepository.deleteById(id);
        return Result.ok();
    }

    @ApiOperation("获得房屋地址信息")
    @ApiImplicitParam(name = "id",value = "房屋Id",required = true,paramType = "Integer")
    @GetMapping("/{id}")
    public Result getAddressById(@PathVariable("id") Integer id)
    {
        House house = houseRepository.findById(id).orElseThrow(() -> new MyException(ResultEnum.HALFSUCCESS));
        return Result.ok(house);
    }

    @ApiOperation("通过aArea获得street信息")
    @ApiImplicitParam(name = "aArea",value = "House实体中的aArea",required = true)
    @PostMapping("/list/street")
    public Result listStreetByAarea(@RequestBody House house) throws Exception
    {
        if (house.getaArea() == null) {
            throw new MyException(ResultEnum.USERDATEEROR);
        }
        System.out.println("\t---->"+house.getaArea());
        List<String> list = houseMapperEx.listStreet(house.getaArea());
        return Result.ok(list);
    }

    @ApiOperation("通过street获得community")
    @ApiImplicitParam(name = "street",value = "House实体中的aArea",required = true)
    @PostMapping("/list/community")
    public Result listCommunityByStreet(@RequestBody House house) throws Exception
    {
        if (house.getStreet() == null) {
            throw new MyException(ResultEnum.USERDATEEROR);
        }
        System.out.println("\t---->"+house.getStreet());
        List<String> list = houseMapperEx.listCommunity(house.getStreet());
        return Result.ok(list);
    }
    @ApiOperation("获得指定条数的house数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "row",value = "每页数据条数",required = true),
            @ApiImplicitParam(name = "page",value = "页码",required = true)
    })
    @GetMapping("/row/{row:\\d+}/page/{page:\\d+}")
    public Result listHouseInPage(@PathVariable("row") Integer row,@PathVariable("page") Integer page)
    {
        if (row < 1 || page < 1) {
            throw new MyException(ResultEnum.USERDATEEROR);
        }
        List<com.lzctzk.serviceaddress.entity.House> houseList = houseMapperEx.listHouseInPage(row * (page - 1), row * page);
        return Result.ok(houseList);
    }
}
